package mest.service.Modeling.Impl;

import com.alibaba.fastjson.JSON;
import mest.VO.Modeling.BomSaveFile_manytomany.savefileVO;
import mest.dao.Modeling.IbomDao;
import mest.entity.bom;
import mest.entity.bomsave_manytomany;
import mest.service.Modeling.IbomService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-16 23:08
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class bomServiceImpl implements IbomService {

    @Resource
    private IbomDao ibomDao;

    @Override
    public String insertBOMFile(bom bom,String theirDb) {
            Map<String,Object> map = new HashMap<String,Object>();
            try {
                ibomDao.insertBOMFile(bom,theirDb);
                map.put("code",0);
                map.put("msg","添加成功");

            } catch (Exception e) {
                e.printStackTrace();
                if (e.getClass().getName().equals("org.springframework.dao.DuplicateKeyException")) {
                    map.put("msg","编码重复");
                    return JSON.toJSONString(map);
                }
                map.put("msg", "添加失败");
            }
            return JSON.toJSONString(map);

    }

    @Override
    public int selectCount(String theirDb) {
        return ibomDao.selectCount(theirDb);
    }

    @Override
    public List<bom> findBOMFilePage(int index, int pageSize,String theirDb) {
        return ibomDao.findBOMFilePage((index-1)*pageSize,pageSize,theirDb);
    }

    @Override
    public List<savefileVO> findSaveByBOMId(int bomid, int index, int pageSize,String theirDb) {
        return ibomDao.findSaveByBOMId(bomid,(index-1)*pageSize,pageSize,theirDb);
    }

    @Override
    public int SaveCount(int id,String theirDb) {
        return ibomDao.SaveCount(id,theirDb);
    }

    @Override
    public int insertManytomany_bomAndSave(bomsave_manytomany bsmtm,String theirDb) {
        return ibomDao.insertManytomany_bomAndSave(bsmtm,theirDb);
    }

    @Override
    public int updateManytomany_bomAndSave(savefileVO bsmtm,String theirDb) {
        return ibomDao.updateManytomany_bomAndSave(bsmtm,theirDb);
    }

    @Override
    public int delManytomany_bomAndSave(bomsave_manytomany bsmtm,String theirDb) {
        return ibomDao.delManytomany_bomAndSave(bsmtm,theirDb);
    }

    @Override
    public String examine(Map<String,Object> map) {
        /* selectById  selectAll  */
        System.out.println("进入查询");
        Map<String,Object> returnmap = new HashMap<String, Object>();
        try {
            Map<String, Object> examine = ibomDao.examine(map);
            if (examine==null){
                returnmap.put("code",0);
                returnmap.put("count",0);
                returnmap.put("msg","可以新增");
            }else {
                returnmap.put("code",0);
                returnmap.put("count",1);
                returnmap.put("msg","已存在");
            }
        }catch (Exception e){
                System.out.println(e.toString());
            returnmap.put("code",1);
            returnmap.put("msg","查询失败");
            returnmap.put("errmsg",e.toString());
        }
        return JSON.toJSONString(returnmap);
    }
}
